//
//  AMPView.m
//  SmartHome
//
//  Created by 赵嘉诚 on 15/3/29.
//  Copyright (c) 2015年 赵嘉诚. All rights reserved.
//

#import "AMPView.h"

@interface AMPView () <UIScrollViewDelegate>
{
    __weak UIButton *hdmi, *hdmi1, *hdmi2, *hdmi3, *hdmi4;
    __weak UIButton *av, *av1, *av2, *av3, *av4;
}

@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation AMPView

- (void)setButton:(UIButton *)button WithImageName:(NSString *)name Title:(NSString *)title TitleColor:(UIColor *)color
{
    UIImage *image = GET_IAMGE_WITH_NAME(name);
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [imageView setFrame:CGRectMake(0, 0, 40, 40)];
    imageView.center = CGPointMake(button.bounds.size.width / 2, button.bounds.size.height / 3);
    [button addSubview:imageView];
    imageView = nil;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
    label.center = CGPointMake(button.bounds.size.width / 2, button.bounds.size.height / 4 * 3);
    label.text = title;
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = color;
    [button addSubview:label];
    label = nil;
}

- (void)clearSubViewsOfButton:(UIButton *)button
{
    NSUInteger count = button.subviews.count;
    for(int i = 0; i < count; i++){
        [button.subviews[0] removeFromSuperview];
    }
}

- (UIScrollView *)scrollView
{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
        _scrollView.delegate = self;
        _scrollView.backgroundColor = BACKGROUND_COLOR;
        _scrollView.scrollEnabled = YES;
    }
    return _scrollView;
}

- (void)addHead
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 10, self.bounds.size.width, self.bounds.size.height / 5)];
    view.backgroundColor = [UIColor whiteColor];
    
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, view.bounds.size.height / 2, view.bounds.size.height / 2)];
    leftButton.center = CGPointMake(35, view.bounds.size.height / 2);
    [leftButton setImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/turnon") forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(pressAMPOn) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, view.bounds.size.height / 2, view.bounds.size.height / 2)];
    rightButton.center = CGPointMake(view.bounds.size.width - 35, view.bounds.size.height / 2);
    [rightButton setImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/turnoff") forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(pressAMPOff) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(view.bounds.size.height / 4 + 55, view.bounds.size.height / 4 - 5, 100, 20)];
    label1.text = @"开启功放";
    label1.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
    label1.textColor = [UIColor blackColor];
    
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(view.bounds.size.height / 4 + 55, view.bounds.size.height / 4 + 20, 150, 40)];
    label2.text = @"只有开启功放才可以控制";
    label2.font = [UIFont fontWithName:@"Arial-BoldMT" size:15];
    label2.textColor = [UIColor lightGrayColor];
    [label2 setLineBreakMode:NSLineBreakByWordWrapping];
    label2.numberOfLines = 0;
    
    [view addSubview:leftButton];
    [view addSubview:rightButton];
    [view addSubview:label1];
    [view addSubview:label2];
    [self.scrollView addSubview:view];
    leftButton = nil;
    rightButton = nil;
    label1 = nil;
    label2 = nil;
    view = nil;
}

- (void)addHDMIButton
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, self.bounds.size.height / 5 + 25, 60, 30)];
    label.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
    label.text = @"输入源";
    label.textColor = TEXT_GRAY_COLOR;
    [self.scrollView addSubview:label];
    label = nil;
    UIButton *button;
    for (int i = 0; i < 4; i++) {
        button = [[UIButton alloc] initWithFrame:CGRectMake(i * ((self.bounds.size.width - 3) / 4 + 1),
                                                            self.bounds.size.height / 5 + 65,
                                                            (self.bounds.size.width - 3) / 4, self.bounds.size.height / 5)];
        button.backgroundColor = [UIColor whiteColor];
        button.tag = i;
        if(i == 0){
            [self setButton:button WithImageName:@"Image/fundationView/gf/hdmi_01_n" Title:@"HDMI1" TitleColor:TEXT_GRAY_COLOR];
            hdmi1 = button;
        }else if(i == 1){
            [self setButton:button WithImageName:@"Image/fundationView/gf/hdmi_02_n" Title:@"HDMI2" TitleColor:TEXT_GRAY_COLOR];
            hdmi2 = button;
        }else if(i == 2){
            [self setButton:button WithImageName:@"Image/fundationView/gf/hdmi_03_n" Title:@"HDMI3" TitleColor:TEXT_GRAY_COLOR];
            hdmi3 = button;
        }else{
            [self setButton:button WithImageName:@"Image/fundationView/gf/hdmi_04_n" Title:@"HDMI4" TitleColor:TEXT_GRAY_COLOR];
            hdmi4 = button;
        }
        [button addTarget:self action:@selector(pressHDMIButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:button];
    }
    button = nil;
}

- (void)addAVButton
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, self.bounds.size.height / 5 * 2 + 80, 100, 30)];
    label.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
    label.text = @"AV输入源";
    label.textColor = TEXT_GRAY_COLOR;
    [self.scrollView addSubview:label];
    label = nil;
    UIButton *button;
    for (int i = 0; i < 4; i++) {
        button = [[UIButton alloc] initWithFrame:CGRectMake(i * ((self.bounds.size.width - 3) / 4 + 1),
                                                            self.bounds.size.height / 5 * 2 + 120,
                                                            (self.bounds.size.width - 3) / 4, self.bounds.size.height / 5)];
        button.backgroundColor = [UIColor whiteColor];
        button.tag = i;
        if(i == 0){
            [self setButton:button WithImageName:@"Image/fundationView/gf/av_01_n" Title:@"AV1" TitleColor:TEXT_GRAY_COLOR];
            av1 = button;
        }else if(i == 1){
            [self setButton:button WithImageName:@"Image/fundationView/gf/av_02_n" Title:@"AV2" TitleColor:TEXT_GRAY_COLOR];
            av2 = button;
        }else if(i == 2){
            [self setButton:button WithImageName:@"Image/fundationView/gf/av_03_n" Title:@"AV3" TitleColor:TEXT_GRAY_COLOR];
            av3 = button;
        }else{
            [self setButton:button WithImageName:@"Image/fundationView/gf/av_04_n" Title:@"AV4" TitleColor:TEXT_GRAY_COLOR];
            av4 = button;
        }
        [button addTarget:self action:@selector(pressAVButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:button];
    }
    button = nil;
}

- (void)addVoiceButton
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, self.bounds.size.height / 5 * 3 + 135, 100, 30)];
    label.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
    label.text = @"操作";
    label.textColor = TEXT_GRAY_COLOR;
    [self.scrollView addSubview:label];
    label = nil;
    UIButton *button;
    for (int i = 0; i < 2; i++) {
        button = [[UIButton alloc] initWithFrame:CGRectMake(i * ((self.bounds.size.width - 1) / 2.0 + 1), self.bounds.size.height / 5 * 3 + 175,
                                                            (self.bounds.size.width - 1) / 2, 50)];
        button.backgroundColor = [UIColor whiteColor];
        button.tag = i;
        if(i == 0){
            UIImage *image = GET_IAMGE_WITH_NAME(@"Image/fundationView/TV/volume_down_p");
            UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
            [imageView setFrame:CGRectMake(0, 0, 35, 35)];
            imageView.center = CGPointMake(button.bounds.size.width / 2, button.bounds.size.height / 2);
            [button addSubview:imageView];
        }else{
            UIImage *image = GET_IAMGE_WITH_NAME(@"Image/fundationView/TV/volume_up_p");
            UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
            [imageView setFrame:CGRectMake(0, 0, 35, 35)];
            imageView.center = CGPointMake(button.bounds.size.width / 2, button.bounds.size.height / 2);
            [button addSubview:imageView];
        }
        [button addTarget:self action:@selector(pressVoiceButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:button];
    }
    button = nil;
    self.scrollView.contentSize = CGSizeMake(self.bounds.size.width, self.bounds.size.height / 5 * 3 + 225);
}

#pragma mark - 按钮响应函数
- (void)pressAMPOn
{
    //开
    [self.delegate sendRespondByFuctionName:AMP_ON];
}

- (void)pressAMPOff
{
    //关
    [self.delegate sendRespondByFuctionName:AMP_OFF];
}

- (void)pressHDMIButton:(UIButton *)button
{
    if(hdmi != nil){
        switch (hdmi.tag) {
            case 0:
                [self clearSubViewsOfButton:hdmi1];
                [self setButton:hdmi1 WithImageName:@"Image/fundationView/gf/hdmi_01_n" Title:@"HDMI1" TitleColor:TEXT_GRAY_COLOR];
                break;
            case 1:
                [self clearSubViewsOfButton:hdmi2];
                [self setButton:hdmi2 WithImageName:@"Image/fundationView/gf/hdmi_02_n" Title:@"HDMI2" TitleColor:TEXT_GRAY_COLOR];
                break;
            case 2:
                [self clearSubViewsOfButton:hdmi3];
                [self setButton:hdmi3 WithImageName:@"Image/fundationView/gf/hdmi_03_n" Title:@"HDMI3" TitleColor:TEXT_GRAY_COLOR];
                break;
            case 3:
                [self clearSubViewsOfButton:hdmi4];
                [self setButton:hdmi4 WithImageName:@"Image/fundationView/gf/hdmi_04_n" Title:@"HDMI4" TitleColor:TEXT_GRAY_COLOR];
                break;
            default:
                break;
        }
    }
    switch (button.tag) {
        case 0:
            [self clearSubViewsOfButton:hdmi1];
            [self setButton:hdmi1 WithImageName:@"Image/fundationView/gf/hdmi_01_p" Title:@"HDMI1" TitleColor:TEXT_GREEN_COLOR];
            //HDMI1
            [self.delegate sendRespondByFuctionName:AMP_HDMI1];
            break;
        case 1:
            [self clearSubViewsOfButton:hdmi2];
            [self setButton:hdmi2 WithImageName:@"Image/fundationView/gf/hdmi_02_p" Title:@"HDMI2" TitleColor:TEXT_GREEN_COLOR];
            //HDMI2
            [self.delegate sendRespondByFuctionName:AMP_HDMI2];
            break;
        case 2:
            [self clearSubViewsOfButton:hdmi3];
            [self setButton:hdmi3 WithImageName:@"Image/fundationView/gf/hdmi_03_p" Title:@"HDMI3" TitleColor:TEXT_GREEN_COLOR];
            //HDMI3
            [self.delegate sendRespondByFuctionName:AMP_HDMI3];
            break;
        case 3:
            [self clearSubViewsOfButton:hdmi4];
            [self setButton:hdmi4 WithImageName:@"Image/fundationView/gf/hdmi_04_p" Title:@"HDMI4" TitleColor:TEXT_GREEN_COLOR];
            //HDMI4
            [self.delegate sendRespondByFuctionName:AMP_HDMI4];
            break;
        default:
            break;
    }
    hdmi = button;
}

- (void)pressAVButton:(UIButton *)button
{
    if(av != nil){
        switch (av.tag) {
            case 0:
                [self clearSubViewsOfButton:av1];
                [self setButton:av1 WithImageName:@"Image/fundationView/gf/av_01_n" Title:@"AV1" TitleColor:TEXT_GRAY_COLOR];
                break;
            case 1:
                [self clearSubViewsOfButton:av2];
                [self setButton:av2 WithImageName:@"Image/fundationView/gf/av_02_n" Title:@"AV2" TitleColor:TEXT_GRAY_COLOR];
                break;
            case 2:
                [self clearSubViewsOfButton:av3];
                [self setButton:av3 WithImageName:@"Image/fundationView/gf/av_03_n" Title:@"AV3" TitleColor:TEXT_GRAY_COLOR];
                break;
            case 3:
                [self clearSubViewsOfButton:av4];
                [self setButton:av4 WithImageName:@"Image/fundationView/gf/av_04_n" Title:@"AV4" TitleColor:TEXT_GRAY_COLOR];
                break;
            default:
                break;
        }
    }
    switch (button.tag) {
        case 0:
            [self clearSubViewsOfButton:av1];
            [self setButton:av1 WithImageName:@"Image/fundationView/gf/av_01_p" Title:@"AV1" TitleColor:TEXT_GREEN_COLOR];
            //AV1
            [self.delegate sendRespondByFuctionName:AMP_AV1];
            break;
        case 1:
            [self clearSubViewsOfButton:av2];
            [self setButton:av2 WithImageName:@"Image/fundationView/gf/av_02_p" Title:@"AV2" TitleColor:TEXT_GREEN_COLOR];
            //AV2
            [self.delegate sendRespondByFuctionName:AMP_AV2];
            break;
        case 2:
            [self clearSubViewsOfButton:av3];
            [self setButton:av3 WithImageName:@"Image/fundationView/gf/av_03_p" Title:@"AV3" TitleColor:TEXT_GREEN_COLOR];
            //AV3
            [self.delegate sendRespondByFuctionName:AMP_AV3];
            break;
        case 3:
            [self clearSubViewsOfButton:av4];
            [self setButton:av4 WithImageName:@"Image/fundationView/gf/av_04_p" Title:@"AV4" TitleColor:TEXT_GREEN_COLOR];
            //AV4
            [self.delegate sendRespondByFuctionName:AMP_AV4];
            break;
        default:
            break;
    }
    av = button;
}

- (void)pressVoiceButton:(UIButton *)button
{
    if(button.tag == 0){
        //音量+
        [self.delegate sendRespondByFuctionName:AMP_VOL_UP];
    }else{
        //音量-
        [self.delegate sendRespondByFuctionName:AMP_VOL_DOWM];
    }
}

- (instancetype) initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        [self addSubview:self.scrollView];
        [self addHead];
        [self addHDMIButton];
        [self addAVButton];
        [self addVoiceButton];
    }
    
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
